package com.baizhi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    //管理员登录页面
    @RequestMapping("/adminLogin")
    public String adminLogin(String username,String password,HttpSession session,String code0){

     //   System.out.println(code0);
        //从session作用域中取出验证码
        LineCaptcha captcha = (LineCaptcha) session.getAttribute("captcha");
    //    System.out.println(captcha);
        //进行验证
        boolean verify = captcha.verify(code0.toLowerCase());
     //   System.out.println(verify);
        if(verify){
            boolean adminLogin = adminService.adminLogin(username, password);
            if(adminLogin){
                Admin admin = adminService.selectAdminByName(username);
                session.setAttribute("admin",admin);
                return "main";
            }else{
                return "login";
            }
        }else{
            return "login";
        }
    }

    //推出登录
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.removeAttribute("admin");
        return "login";
    }


    @RequestMapping("/getCaptcha")
    public void getCaptcha(HttpSession session, HttpServletResponse response) throws IOException {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
//        System.out.println(lineCaptcha.getCode());
        //将验证码对象lineCaptcha存到session作用域中
        session.setAttribute("captcha",lineCaptcha);
        lineCaptcha.write(response.getOutputStream());
    }


    //查询所有管理员  分页+条件
    @RequestMapping("/listAdmin")
    public @ResponseBody Map listAdmin(int page, int limit, String name){
        return adminService.listAdmin(page,limit,name);
    }
    //添加管理员
    @RequestMapping("/insertAdmin")
    public @ResponseBody Map insertAdmin(Admin admin){
        return adminService.insertAdmin(admin);
    }
    //查询一个管理员对象
    @RequestMapping("/selectAdminById")
    public Admin selectAdminById(Integer id){
        return adminService.selectAdminById(id);
    }
    //修改管理员
    @RequestMapping("/updateAdmin")
    @ResponseBody
    public Map updateAdmin(Admin admin){
        return adminService.updateAdmin(admin);
    }
    //批量删除管理员
    @RequestMapping("/multiDeleteAdmin")
    @ResponseBody
    public Map multiDeleteAdmin(Integer[] ids){
        return adminService.multiDeleteAdmin(ids);
    }
}
